package com.fortune.rainshine.controller;

import com.fortune.rainshine.feign.service.MessageFeign;
import com.fortune.rainshine.feign.system.UserFeign;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.base.ParmsModel;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.service.MessageModel;
import com.fortune.rainshinecommon.model.system.User;
import com.fortune.rainshinecommon.query.MessageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("MessageController")
public class MessageController {
    private static final Logger logger = LoggerFactory.getLogger(MessageController.class);

    @Autowired
    private UserFeign userFeign;
    @Autowired
    private MessageFeign messageFeign;

    @PostMapping("/updateState")
    @ResponseBody
    public MsgModel updateState(@RequestBody MessageQuery query){
        if(query.getUserId() == null){
            return new MsgModel("登录用户异常",ResultCode.FAILURE);
        }
        if(query.getMessageId() == null){
            return new MsgModel("请选择一条信息",ResultCode.FAILURE);
        }
        User user = userFeign.getUserById(query.getUserId());
        if(user == null){
            return new MsgModel("此用户不存在",ResultCode.FAILURE);
        }
        MessageModel messageModel = messageFeign.findByid(query.getMessageId());
        if(messageModel == null){
            return new MsgModel("预警信息不存在",ResultCode.FAILURE);
        }
        if(messageModel.getUserId() != query.getUserId()){
            return new MsgModel("无操作权限",ResultCode.FAILURE);
        }
        try{
            messageFeign.updateState(query.getMessageId());
            return new MsgModel("修改状态成功",ResultCode.SUCCESS);
        }catch (Exception e){
            return new MsgModel("预警信息不存在",ResultCode.FAILURE);
        }
    }

    @PostMapping("/list")
    @ResponseBody
    public MsgModel productList(@RequestBody ParmsModel parm){
        parm.setSearchValue("0");
        return messageFeign.productList(parm);
    }

}
